package dao.impl;

import dao.UserinfoDao;
import entity.Userinfo;
import utils.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class ElseinfoDaoImpl implements UserinfoDao {
    @Override
    public List<Userinfo> queryAllUserinfo() {
        List<Userinfo> list=new ArrayList<>();
        Connection conn= DBUtil.getConnection();
        PreparedStatement stat=null;
        ResultSet res =null;
        String sql="select * from Userinfo";
        try {
            stat=conn.prepareStatement(sql);
            res=stat.executeQuery();
            while (res.next()){
                int ucode=res.getInt(1);
                String uname=res.getString(2);
                String uid=res.getString(3);
                String upassword=res.getString(4);
                String uidcard=res.getString(5);
                String usex=res.getString(6);
                String uphone=res.getString(7);
                String ublack=res.getString(8);
                Date utime=res.getDate(9);
                Userinfo user =new Userinfo(ucode,uname,uid,upassword,uidcard,usex,uphone,ublack,utime);
                list.add(user);
            }
        } catch (SQLException e) {
           e.printStackTrace();
        }finally {
            DBUtil.close(conn,stat,res);
        }
        return list;
    }

    @Override
    public Userinfo queryUserinfo(int ucode) {
        Userinfo userinfo =new Userinfo();
       Connection conn=DBUtil.getConnection();
       PreparedStatement stat =null;
       ResultSet res=null;
       String sql ="select * from Userinfo where Ucode = ?";
       try {
           stat = conn.prepareStatement(sql);
           stat.setInt(1,ucode);
            res=stat.executeQuery();
            while (res.next()){
                int ucod=res.getInt(1);
                String uname=res.getString(2);
                String uid=res.getString(3);
                String upassword=res.getString(4);
                String uidcard=res.getString(5);
                String usex=res.getString(6);
                String uphone=res.getString(7);
                String ublack=res.getString(8);
                Date utime=res.getDate(9);
                userinfo.setUcode(ucod);
                userinfo.setUname(uname);
                userinfo.setUid(uid);
                userinfo.setUpassword(upassword);
                userinfo.setUidcard(uidcard);
                userinfo.setUsex(usex);
                userinfo.setuPhone(uphone);
                userinfo.setUblack(ublack);
                userinfo.setUtime(utime);
            }

       }catch (SQLException e){
           e.printStackTrace();
       }finally {
           DBUtil.close(conn,stat,res);
       }
       return userinfo;
    }

    @Override
    public int addUser(Userinfo user) {
        int r = 0;
        Connection conn =DBUtil.getConnection();
        PreparedStatement stat =null;
        ResultSet res=null;
        String sql = "insert into Userinfo(Uname,Uid,Upassword,Uidcard,Usex,Uphone,Ublack,Utime) values (?,?,?,?,?,?,?,?)" ;
        try{
            stat=conn.prepareStatement(sql);
            stat.setInt(1,user.getUcode());
            stat.setString(2,user.getUid());
            stat.setString(3,user.getUpassword());
            stat.setString(4,user.getUidcard());
            stat.setString(5,user.getUsex());
            stat.setString(6,user.getuPhone());
            stat.setString(7,user.getUblack());
            stat.setDate(8, (java.sql.Date) user.getUtime());
            r=stat.executeUpdate();
        }catch (SQLException e){
            e.printStackTrace();
        }finally {
            DBUtil.close(conn,stat,null);
        }
        return r;
    }

    @Override
    public int delUser(int ucode) {
        int r = 0 ;
        Connection conn = DBUtil.getConnection();
        PreparedStatement stmt = null ;
        String sql = "delete from Userinfo where Ucode=?" ;
        try {
            stmt = conn.prepareStatement(sql);
            stmt.setInt(1,ucode);
            r = stmt.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(conn,stmt,null);
        }
        return r;
    }

    @Override
    public int updateUser(Userinfo user) {
        int r=0;
        Connection conn=DBUtil.getConnection();
        PreparedStatement stat =null;
        String sql = "update Userinfo set Uname=?,Upassword=?,Uidcard=?,Usex=?,Uphone=?,Ublack=? where Ucode=?" ;
        try{
            stat=conn.prepareStatement(sql);
            stat.setString(1,user.getUname());
            stat.setString(2,user.getUpassword());
            stat.setString(3,user.getUidcard());
            stat.setString(4,user.getUsex());
            stat.setString(5,user.getuPhone());
            stat.setString(6,user.getUblack());
            stat.setInt(7,user.getUcode());
            r=stat.executeUpdate();
        }catch (SQLException e){
            e.printStackTrace();
        }finally {
            DBUtil.close(conn,stat,null);
        }
        return r;
    }
}
